<?php

declare(strict_types=1);

namespace App\AdminApi\Module\Admin\Api;

use App\AdminApi\AdminApiBasicsController;
use App\AdminApi\Module\Admin\Service\MenuService;
use App\AdminApi\Module\Admin\Validate\MenuValidate;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\{Controller, GetMapping, PostMapping, RequestMapping};
use Psr\Http\Message\ResponseInterface;
use Zms\AdminElementPlus\FormCreate\ElemenuForm;
use Zms\Guard\Auth;
use Zms\HyperfResponse\Response;
use Zms\HyperfValidateAnnotations\Validate;

#[
    Controller(prefix: "/admin-api/admin-menu", server: self::SERVER),
    Auth(guard: self::GUARD, label: ['后台管理', '菜单'], app: self::APP)
]
class MenuController extends AdminApiBasicsController
{
    #[Inject]
    protected MenuService $service;

    #[
        GetMapping('data'),
        Auth(label: ['查看,管理'], sign: 'admin.menu.data', view: '/admin/menu/index')
    ]
    public function data(): ResponseInterface
    {
        $data = $this->service->getMenuData();
        return Response::Api()->success(data: $data);
    }


    #[
        RequestMapping('created'),
        Auth(sign: 'admin.menu.created', logger: "POST", record: ['name'], describe: "新建后台菜单"),
        ElemenuForm(class: MenuService::class, function: "createdMenuForm", param: ["pid"]),
        Validate(class: MenuValidate::class, scene: "created")
    ]
    public function created(): ResponseInterface
    {
        $data = $this->request->getParsedBody();
        if ($this->service->created($data)) {
            return Response::Api()->success('菜单添加成功');
        }
        return Response::Api()->fail('菜单添加失败');
    }


    #[
        RequestMapping('updated'),
        Auth(sign: 'admin.menu.updated', logger: "POST", record: ['id', 'name']),
        ElemenuForm(class: MenuService::class, function: "updatedMenuForm", param: ["id"]),
        Validate(class: MenuValidate::class, scene: "updated")
    ]
    public function updated(): ResponseInterface
    {
        $data = $this->request->getParsedBody();
        if ($this->service->updated($data)) {
            return Response::Api()->success('菜单编辑成功');
        }
        return Response::Api()->fail('菜单编辑失败');

    }


    #[
        PostMapping('status'),
        Auth(relyon: ["admin.menu.updated"], logger: false, describe: '修改菜单显示状态'),
        Validate(rules: \Zms\Constants\Validate::CHANGE_STATUS_RULES)
    ]
    public function status(): ResponseInterface
    {
        $data = $this->request->inputs(['id', 'status']);
        if ($this->service->search(['id' => $data['id']])->update(['is_nav' => $data['status']])) {
            return Response::Api()->success('更改成功');
        }
        return Response::Api()->fail('更改失败');
    }


    #[
        PostMapping('remove'),
        Auth(sign: 'admin.menu.remove', describe: '删除菜单'),
    ]
    public function remove(): ResponseInterface
    {
        $id = (int)$this->request->input('id', 0);
        $result = $this->service->search()->whereRaw("(FIND_IN_SET('$id',`group`) OR `id`='$id')")->delete();
        if ($result) {
            return Response::Api()->success('菜单已删除');
        }
        return Response::Api()->fail('没有删除成功的菜单');
    }
}